package cn.livingCloud.controller.core.validator;

import cn.livingCloud.dto.DeptFormDto;
import cn.livingCloud.utils.HtmlRegexpUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by DingJie  on 2016/5/27.
 */
@Component
public class DeptFormDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return DeptFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        DeptFormDto formDto = (DeptFormDto) target;
        validateTitleAndContent(formDto, errors);
    }

    private void validateTitleAndContent(DeptFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getName())) {
            errors.rejectValue("name", null, "部门不能为空");
        }
        if (HtmlRegexpUtil.hasSpecialChars(formDto.getName())) {
            errors.rejectValue("name", null, "部门名称不能有特殊字符");
        }
        if (formDto.getName().length() > 100) {
            errors.rejectValue("name", null, "部门名称不能超过100个字符");
        }
    }
}
